package com.cty._01_Base._15_NumberOf1InBinary;

/**
 * @Auther: cty
 * @Date: 2020/7/4 9:49
 * @Description:
 * @version: 1.0
 */
public class NumberOf1InBinary {
    /**
     * 移位与运算实现
     * 时间复杂度：O(N)
     * 空间复杂度：O(1)
     * @param n
     * @return
     */
    public static int numberOf1_01(int n){
        int count = 0;
        int flag = 1;

        while(flag != 0){
            if((n & flag) != 0)
                count++;

            flag = flag << 1;
        }

        return count;
    }  // end numberOf1_01()

    /**
     * 减一与自己与运算实现
     * @param n
     * @return
     */
    public static int numberOf1_02(int n){
        int count = 0;
        while(n != 0){
            count++;
            n = (n-1) & n;
        }

        return count;
    }  // end numberOf1_02()

    public static void main(String[] args) {
        long flag = 1;
        long n = 9;
        System.out.println(n&flag);
        flag = flag << 3;
        System.out.println(n&flag);
    }

}  // end class
